import { $wuxButton } from '../../components/wux'
var App = getApp()
/**
 * 关于：user/about
 * 签到：user/sign
 */

Page({

  data: {
    about: '',
    types: ['topLeft', 'topRight', 'bottomLeft', 'bottomRight'],
    index: 3,
    opened: !1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.initButton()
    this.about = App.HttpResource('user/about/:id', { id: '@id' })
    this.sign = App.HttpResource('user/sign/:id', { id: '@id' })
  },

  initButton(position = 'bottomRight') {
    var that = this
    this.setData({
      opened: !1,
    })

    this.button = $wuxButton.init('br', {
      position: position,
      buttons: [
        {
          label: '继续看',
          icon: "",
        },
        {
          label: '签到',
          icon: "",
        }
      ],
      buttonClicked(index, item) {
        index === 0 && wx.navigateBack({
          delta: 1
        })
        //刷新数据
        index === 1 && that.userSign()
      },
      callback(vm, opened) {
        vm.setData({
          opened,
        })
      },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this
    this.about.queryAsync({})
      .then(res => {
        that.setData({
          about: res.data.data
        })
      })
  },
  check() {
    if (App.Auth.check) return true;
    //登录
    App.wxLogin()
      .then(res => {
        App.Auth.storageUser(res.data)
        return true;
      })
  },
  userSign() {
    if (this.check()) {
      this.sign.queryAsync({})
        .then(res => {
          var res = res.data
          if (res.errno == 401) {
            //已签到或是vip
            wx.navigateBack({
              delta: 1
            })
          }
          if (res.errno == 0) {
            //增加时间
            wx.setStorageSync('overtime', res.data * 1000)
            wx.showModal({
              title: '签到成功',
              content: '已为您解锁部分内容',
              showCancel: false,
              confirmText: '快去看',
              success: function (res) {
                if (res.confirm) {
                  wx.navigateBack({
                    delta: 1
                  })
                }
              },
            })
          }

        })
    }
  }
})